const tool = require('../tools/tool')

module.exports = {
    async import(categoryId, name, list, isEssay) {
        if ((await this.getSingle.byName(categoryId, name, false)) !== undefined) {
            throw '已有同名试卷'
        }
        let paperId = await tool.dbf().insertInto('paper', {
            categoryId,
            name,
            duration: 120,
            isEssay
        })
        for (let i = 0; i < list.length; i++) {
            await tool.methods.question().add(categoryId, paperId, list[i])
        }
    },
    getSingle: {
        async byId(id, format = true) {
            let re = await tool.dbf().selectWithParams(`select * from paper where id = ?`, [id])
            if (format) return re[0]
            return await this.format(re[0])
        },
        async byName(categoryId, name, format = true) {
            let re = await tool.dbf().selectWithParams(`select * from paper where name = ? and categoryId = ?`, [name, categoryId])
            if (format) return re[0]
            return await this.format(re[0])
        },
        async format(data) {
            if (data === undefined) return undefined
            data.questions = await tool.methods.question().getList(data.id)
            data.category = await tool.methods.category().getSingle(data.categoryId)
            return data
        }
    },
    getList: {
        async byCategory(categoryId) {
            let re = await tool.dbf().selectWithParams(`select * from paper where categoryId = ?`, [categoryId])
            return re
        }
    },
    async delete(paperId) {
        await tool.dbf().exeWithParams(`delete from question where paperId = ?`, [paperId])
        await tool.dbf().deleteById('paper', paperId)
    },
    async getRandomPaperIdByCategoryId(userId, categoryId) {
        let examCache = await tool.methods.exam().getUserCategoryExamCache(userId, categoryId)
        let papers = await this.getList.byCategory(categoryId)
        for (let i = 0; i < examCache.length; i++) {
            for (let i1 = 0; i1 < papers.length; i1++) {
                if (papers[i1].id === examCache[i].paperId) {
                    papers[i1].pass = true
                    break
                }
            }
        }
        let undoPaperIds = []
        for (let i = 0; i < papers.length; i++) {
            if (!papers[i].pass) undoPaperIds.push(papers[i].id)
        }
        if (undoPaperIds.length) {
            return undoPaperIds[parseInt(Math.random() * 100) % undoPaperIds.length]
        }
        return papers[parseInt(Math.random() * 100) % papers.length].id
    },
    async isLastQuestion(paperId, questionId) {
        let paperInfo = await this.getSingle.byId(paperId)
        if (!paperInfo) throw '试卷不存在'
        let questionInfo = await tool.methods.question().getSingle(questionId)
        if (!questionInfo) throw '问题不存在'
        let count = (await tool.dbf().selectWithParams(`select count(id) as total from question where paperId = ?`, [paperId]))[0].total
        if (questionInfo.index === count) {
            return true
        }
        return false
    },
    async getQuestionNum(paperId) {
        return (await tool.dbf().selectWithParams(`select count(id) as total from question where paperId = ?`, [paperId]))[0].total
    }
}